/**
 * @Author: linzp
 * @Date: 2021/08/24/11:02
 * 抽象工厂模式的缺点：如果需要增加新的产品树，那么就要新增三个产品类，
 * 比如VolvoCar，VolvoSportCar,VolvobusinessCar，
 * 并且要修改三个工厂类。这样大批量的改动是很丑陋的做法。
 *
 * 所以可以用简单工厂配合反射来改进抽象工厂：
 */
public class factory2 {
    public static void main(String[] args) throws Exception {

        AudiCar car = Driver3.createAudiCar("com.java.pattendesign.factory.AudiSportCar");
        car.drive();
    }
}
abstract class BenzCar{
    private String name;

    public abstract void drive();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
class BenzSportCar extends BenzCar{
    @Override
    public void drive(){
        System.out.println(this.getName()+"----BenzSportCar-----------------------");
    }
}
class BenzBusinessCar extends BenzCar{
    @Override
    public void drive(){
        System.out.println(this.getName()+"----BenzBusinessCar-----------------------");
    }
}

abstract class BmwCar{
    private String name;

    public abstract void drive();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
class BmwSportCar extends BmwCar{
    @Override
    public void drive(){
        System.out.println(this.getName()+"----BmwSportCar-----------------------");
    }
}
class BmwBusinessCar extends BmwCar{
    @Override
    public void drive(){
        System.out.println(this.getName()+"----BmwBusinessCar-----------------------");
    }
}

abstract class AudiCar{
    private String name;

    public abstract void drive();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
class AudiSportCar extends AudiCar{
    @Override
    public void drive(){
        System.out.println(this.getName()+"----AudiSportCar-----------------------");
    }
}
class AudiBusinessCar extends AudiCar{
    @Override
    public void drive(){
        System.out.println(this.getName()+"----AudiBusinessCar-----------------------");
    }
}


/**
 * 简单工厂通过反射改进抽象工厂及其子工厂
 * @author Administrator
 *
 */
class Driver3{
    public static BenzCar createBenzCar(String car) throws Exception {
        return (BenzCar) Class.forName(car).newInstance();
    }

    public static BmwCar createBmwCar(String car) throws Exception {
        return (BmwCar) Class.forName(car).newInstance();
    }

    public static AudiCar createAudiCar(String car) throws Exception {
        return (AudiCar) Class.forName(car).newInstance();
    }
}
